package com.jackrain.nea.shiro.stateful;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.SessionListener;
import org.junit.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * Created by Lucky dog on 2016/7/19.
 */
@Component
public class ShiroSessionListener implements SessionListener {

    private static Logger logger= LoggerFactory.getLogger(ShiroSessionListener.class.getName());

    @Autowired
    private CachingShiroSessionDao sessionDao;

    @Override
    public void onStart(Session session) {
        // 会话创建时触发
        Assert.assertNotNull(sessionDao);
        logger.info("ShiroSessionListener session 被启动:"+session.getId());
    }

    @Override
    public void onStop(Session session) {
        sessionDao.onDelete(session);
        // 会话被停止时触发
        logger.info("ShiroSessionListener session 被销毁:"+session.getId());
    }

    @Override
    public void onExpiration(Session session) {
        sessionDao.onDelete(session);
        //会话过期时触发
        logger.info("ShiroSessionListener session 过期:"+session.getId());
    }
}
